<?php
include 'functions/functions.php';
include 'functions/mysql.php';
session_start();
$links = CreateLinkArray();
if(!isset($_GET["a"]) || !array_key_exists($_GET['a'],$links)){
	$_GET['a'] = "news";
}
if(!isset($_GET['page']))  {
	$_GET['page'] = '0';
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- Facebook button -->
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/sv_SE/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<!-- Google button -->
<script type="text/javascript">
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
</script>
<!-- Google Analytics -->
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-27696106-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<!-- Graphs -->
<script src="/libraries/RGraph.common.core.js"></script>
<script src="/libraries/RGraph.hbar.js"></script>
<link href='http://fonts.googleapis.com/css?family=Fascinate'
	rel='stylesheet' type='text/css'>
	<script>
    window.onload = function ()
    {	
	<?	
		if(isset($_SESSION['username']) && ($_GET['a'] == 'my-profile')) {
			if(!isset($_GET['topartu'])){
				$_GET['topartu'] = 10;
			}
			
			displayUserTop('artists',$_GET['topartu'],'topartu');
			
			if(!isset($_GET['topsongu'])){
				$_GET['topsongu'] = 10;
			}
			displayUserTop('songs',$_GET['topsongu'],'topsongu');
		}
		
 		displayGlobalTopAside('artists',5,'top5artistaside');
 		displayGlobalTopAside('songs',5,'top5songaside');
		if($_GET['a'] == 'statistics') {
			if(!isset($_GET['topart'])){
				$_GET['topart'] = 10;
			}
			displayGlobalTop('artists',$_GET['topart'],'topart');
			
			if(!isset($_GET['topsong'])){
				$_GET['topsong'] = 10;
			}
			displayGlobalTop('songs',$_GET['topsong'],'topsong');
		}
		

		
	?>
    }

</script>


	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" media="screen" href="master.css">
		<title>Myms</title>

</head>
<body>
	<header id="siteheader">
	<h1>My Music Statistics</h1>
	<? if(!isset($_SESSION['admin'])) {
		echo '
			<form id="loginform" method="post" action="functions/login.php">
				<fieldset id="inputs">
					<input placeholder="Username" name="username" required/>
					<input type="password" placeholder="Password" name="password" required/>
				</fieldset>
				<fieldset id="actions">
					<input type="submit" name="login" value="Log in" />
					<a href="?a=register">Register</a>
					<a href="?a=forgot">Forgot password?</a>
				</fieldset>
			</form>';
	} else {
		echo '<div id="actions">';
		echo "Logged in as <b>".$_SESSION['username']."</b>&nbsp;<br />";
		echo '<a href="functions/logout.php">Logout</a>';
		if(isset($_SESSION['admin']) && $_SESSION['admin'] == 1) {
			echo' <a href="/?a=admin">Administrate</a>';
		}
		echo '</div>';
	}
	?> </header> <nav>
	<ul>
	<?	//Automatically creates the navigation menu
	CreateMenu(); ?>
	</ul>
	</nav> <article id="mainContent"> <? include ($links[$_GET['a']]); ?> </article>
	<aside> 
   
    <header>Search for artist</header>
	<form id="searchform" method="get" action="">
		<fieldset>
			<input type="hidden" name="a" value="artist" /> <input
				placeholder="Artist name" name="artist" required /> <input
				class="formbtn" type="submit" name="search" value="Search" />
		</fieldset>
	</form>
     <? if(isset($_SESSION['username'])) {
		 $user = $_SESSION['username'];
  		 echo'<header>Last played</header>';
		 $client = new RestClient;
		 //Retrieves artists/songs from database using our API
		 $lastplayed = json_decode($client->get("/api/users/$user/lastplayed"), true);
		 echo '<section id="lastplayedaside">';
		 if($lastplayed == "") {
			 echo "You haven't listened to any track yet.";
		 } else {
			 print_r($lastplayed[1]['lastplayed']);
		 }
		 echo '</section>';
		 
	}?>
	<header>Top 5 Artists</header> <canvas id="top5artistaside"></canvas> <header>Top
	5 Songs</header> <canvas id="top5songaside"></canvas> </aside> 
	<footer id="sitefooter">
	&copy; John Ek &amp; Patrik Burstr&ouml;m 2011
	<div class="g-plusone" data-href="www.fleskdoris.se"></div>
	<div class="fb-like" data-send="false" data-layout="button_count"
		data-width="20" data-show-faces="false"></div>
	<a href="https://twitter.com/share" class="twitter-share-button"
		data-lang="en">Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script></footer>
</body>
</html>
